package de.szut.ita13sommerfeld.apacheanalyser.handler;

import de.szut.ita13sommerfeld.apacheanalyser.apachelog.Date;
import de.szut.ita13sommerfeld.apacheanalyser.apachelog.Entry;
/**
 * 
 * @author Rene Sommerfeld
 *
 */
public class LineParser {

	private static final int LOGLINE_PREFIX = 10;
	
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil IP parsed out of the logline
	 */
	public static String getIP(String logline) {
		return logline.split(" ")[0];
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Hyphen parsed out of the logline
	 */
	public static String getHyphen(String logline) {
		return logline.split(" ")[1];
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil ID parsed out of the logline
	 */
	public static String getID(String logline) {
		return logline.split(" ")[2];
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Date parsed out of the logline
	 */
	public static String getDate(String logline) {
		return logline.substring(logline.indexOf("[") + 1, logline.indexOf("]"));
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Method parsed out of the logline
	 */
	public static String getMethod(String logline) {
		return logline.split(" ")[5].substring(1,logline.split(" ")[5].length());
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Resource parsed out of the logline
	 */
	public static String getResource(String logline) {
		return logline.split(" ")[6];
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Protocol parsed out of the logline
	 */
	public static String getProtocol(String logline) {
		return logline.split(" ")[7].substring(0, logline.split(" ")[7].length() - 1);
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Status parsed out of the logline
	 */
	public static String getStatus(String logline) {
		return logline.split(" ")[8];
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns the specifil Size parsed out of the logline
	 */
	public static String getSize(String logline) {
		return (logline.split(" ")[9].equals("-")) ? logline.split(" ")[9].replace("-", "0") : logline.split(" ")[9];
	}
	/**
	 * @param logline a specific logline of the log
	 * @return returns an Entry created through all of the parsing methods results 
	 */
	public static Entry getEntryFromLogLine(String logline) {
		Entry entry = null;
		if(logline.split(" ").length == LOGLINE_PREFIX) {
			String ip = getIP(logline);
			String hyphen = getHyphen(logline);
			String id = getID(logline);
			String date = getDate(logline);
			String method = getMethod(logline);
			String resource = getResource(logline);
			String protocol = getProtocol(logline);
			String status = getStatus(logline);
			String size = getSize(logline);
			entry  = new Entry(ip,hyphen,id, new Date(date),method,resource,protocol,Integer.parseInt(status),Integer.parseInt(size));
		}
		return entry;
	}

}
